package cn.itcast;

import cn.itcast.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;

/**
 * @author zhangzhuang@123456
 * @DATE 2018/11/6
 * @TIME 19:54
 */
public class Demotest {
    public static void main(String[] args) throws Exception {
        //1.读取核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlConfig.xml");
        //2.创建SqlSessionFactory对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(inputStream);
        //3.获取SqlSession对象
        SqlSession session = factory.openSession();
        //4.获取Mapper对象
        UserDao dao = session.getMapper(UserDao.class);
        //5.执行sql

//        List<User> list = dao.findAll();
//        for (User user : list) {
//            System.out.println(user);
//        }

        User user = new User();
//        user.setUsername("孙天亮");
//        user.setBirthday(new Date());
//        user.setSex("女");
//        user.setAddress("肥西");
//
//        dao.addUser(user);


//        user.setId(51);
//        user.setUsername("孙天亮");
//        user.setBirthday(new Date());
//        user.setSex("男");
//        user.setAddress("肥西");
//
//        dao.updateUser(user);


//        User userById = dao.findUserById(51);
//        System.out.println(userById);

//        user.setId(43);
//        user.setAddress("合肥");
//
//        User userone = dao.findOne(user);
//        System.out.println(userone);


        dao.deleteUserById(51);


//        int num = dao.num();
//        System.out.println(num);

        //6.提交事务
        session.commit();
        //7.释放资源
        session.close();
        inputStream.close();
    }


}
